/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Entity;

import com.mysql.jdbc.Driver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.StringTokenizer;

/**
 *
 * @author Ardha
 */
public class Koneksi {

    private static Connection koneksi;

    public Koneksi() {
    }

    public static Connection getDBConnection() throws SQLException, FileNotFoundException, IOException {
        String result[] = Koneksi.openConfigurationFile(new File("db.conf"));
        String jdbcUrl="jdbc:mysql://"+result[0]+":3306/pti";
        if (koneksi == null) {
            Driver driver = new Driver();
            koneksi = DriverManager.getConnection(jdbcUrl, result[1], result[2]);
        }
        return koneksi;
    }

    public static String[] openConfigurationFile(File file) throws FileNotFoundException, IOException {
        FileInputStream input = new FileInputStream(file);
        String teks = new String();
        int c = 0;
        while ((c = input.read()) != -1) {
            teks += (char) c;
        }

        StringTokenizer tokens = new StringTokenizer(teks, ";");
        String result[] = new String[tokens.countTokens()];

        for (int i = 0; i < result.length; i++) {
            result[i] = tokens.nextToken();
        }

        return result;
    }
}
